var app = getApp();
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var GZHid=53;//公众号id
var page = 1;
var pagesize = 4;
var dataLists={};//缓存商品列表数据
Page({
	data: {
		currentNavtab: '0',
		navbarTop:[
	      // {id:'0', title:'首页'},
	      // {id:'1', title:'服饰'},
	      // {id:'2', title:'男装'},
	      // {id:'3', title:'母婴'},
	      // {id:'4', title:'家居'},
	      // {id:'5', title:'美食'},
	      // {id:'6', title:'电器'},
	      // {id:'7', title:'家纺'},
	      // {id:'8', title:'美妆'},
	      // {id:'9', title:'水果'}
    	],
		catboard: [
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'女装'},
			// { image: '',text:'更多'}
		],
		goodsList: [
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'},
			// {image:'',description: '【YBUGQI官方正品】2017新款羊皮质感双肩女背包【买一得三】',price: '23.9',stock:'7.2'}
		]
	},
	onLoad: function (option) {
		page = 1;   //初始化page
		var _id = '';  //判断从搜索进来,还是navbar进来
		var that = this;
		this.setData({
			currentNavtab: option.id,
			cid: option.id
		})
		wx.request({   						//导航头
			url: commonURLYY+'op=fenlei',
			header: {
	        'content-type': 'application/json'
	      	},
	      	success: function (res) {
	      		that.setData({
	      			navbarTop: res.data.menuList
	      		})
	      	}
		})
		if(option.child){
			_id = option.child;
		}else{
			_id = option.id;
		}
		this.getfenleiTwo(option.id);
		this.getproductList(_id,page,pagesize);
	},
	onShow: function () {
		wx.request({
	      url:commonURLXX+ 'op=uniacid_shop',
	      header: {
	        'content-type': 'application/json'
	      },
	      success:function (res) {
	         wx.setNavigationBarTitle({title:res.data.sname});//设置title文字
	      }
	    })
	},
	getfenleiTwo: function (id) {
		var that = this;
		wx.request({ 						//分类小图
			url: commonURLYY+'op=fenleiTwo',
			data: {
				cid: id
			},
			header: {
	        'content-type': 'application/json'
	      	},
			success: function (res) {
				that.setData({
					catboard : res.data.productList,
				})
			}
		})
	},
	getproductList: function (id,page,pagesize) {
		var that = this;
		wx.request({   						//商品列表
			url: commonURLYY+'op=ajax',
			data: {
				cid: id,
				page: page,
				pagesize: pagesize	
			},
			header: {
				'content-type': 'application/json'
			},
			success: function (res) {
				that.setData({
					goodsList: res.data.list
				})
			}
		})
	},
	goNavBarDetail: function (e) {
		page = 1;  //每次跳转初始化
		var id = e.currentTarget.dataset.id;
		var index = e.currentTarget.dataset.index; 
		this.setData({
			currentNavtab: id,
			cid: id,
			goodsList: []
		})
		if(index == '-1'){
			wx.navigateBack({
			  delta: 1
			})
		}
		this.getfenleiTwo(id);
		this.getproductList(id,page,pagesize);
	},
	onfenleiTwo: function (e) {
		page = 1;	//每次点击子分类初始化
		var _id = e.currentTarget.dataset.id;
		this.getproductList(_id,page,pagesize);
	},
	onReachBottom: function () {
		var that = this;
		page++;
		wx.request({
			url: commonURLYY + 'op=ajax',
			data: {
				cid: this.data.cid,
				page: page,
				pagesize: pagesize
			},
			header: {
				'content-type': 'application/json'
			},
			success: function (res) {
				if(res.data.list.length < 1) {
					wx.showToast({
						title: '全部商品加载完成',
						icon: 'success',
						duration: 1000
					})
					return false;
				}
				wx.showToast({
					title: '商品加载中...',
					icon: 'loading',
					duration: 700
				})
				var i = 0,lists = [],length = res.data.list.length;
				for(;i < length; i++ ) {
					var obj = {};
					var indexid = obj["id"] = res.data.list[i].id;
					obj['gimg'] = res.data.list[i].gimg;
					obj['gname'] = res.data.list[i].gname;
					obj["price"]=res.data.list[i].oprice;//单买价
					obj["gprice"]=res.data.list[i].gprice;//团购价
					obj["peoplenum"]=res.data.list[i].groupnum;
					obj["unit"]=res.data.list[i].unit;
		            obj["mprice"]=res.data.list[i].mprice;//市场价
		            obj["salenum"]=res.data.list[i].salenum;
		            obj["xiangqing"]=res.data.list[i].gdesc;
		            obj["shuxing"]=res.data.list[i].params;
		            obj["advs"]=res.data.list[i].advs;
		            obj["kucun"]=res.data.list[i].gnum;
		            obj["selltype"]=res.data.list[i].selltype;
		            obj["hasoption"]=res.data.list[i].hasoption;//是否有规格 0没有 ，1有
		            obj["weight"]=res.data.list[i].weight;//重量，用于计算运费
		            obj["deliverytype"]=res.data.list[i].deliverytype;//配送方式
					if(res.data.list[i].gnum=="0"){
		              obj["smtuan"]="售罄";
		              obj["backg"]="#aaa";
		              obj["url"]='';
		            }else{
		              obj["smtuan"]="随意团  >";
		              obj["url"]='../index_detail/index_detail?id='+indexid;
		            }
		            lists.push(obj);
            		dataLists[indexid]=obj;
				}
				that.setData({
		            goodsList:that.data.goodsList.concat(lists)
		        });
		        wx.getStorage({
		        	key: 'goodsLists',
		        	success: function (res) {
		        		var data = res.data[indexid];
		        		if(!res.data[indexid]) { //如果缓存中不存在对应的商品，就ajax加载数据
		        			wx.setStorage({
			                  key:"goodsLists",
			                  data:dataLists
			                })
		        		} 
		        	} 
		        })
			}
		})
	},
	go_detail: function (e) {    //去商品详情页
		var _id = e.currentTarget.dataset.id;
		var _selltype = e.currentTarget.dataset.selltype;
		if(_selltype == '0'){
			wx.navigateTo({
				url: '../index_only_detail/index_only_detail?id='+_id
			})
		}else{
			wx.navigateTo({
				url: '../index_detail/index_detail?id='+_id
			})
		}
	}
})